package cn.com.aiouyi.service.impl;

import cn.com.aiouyi.common.ExceptionCode;
import cn.com.aiouyi.dao.PluginConfigDao;
import cn.com.aiouyi.entity.PluginConfig;
import cn.com.aiouyi.exception.business.BusinessException;
import cn.com.aiouyi.page.Filter;
import cn.com.aiouyi.service.PluginConfigService;
import cn.com.aiouyi.util.LogHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Service - 插件配置
 */
@Service("pluginConfigServiceImpl")
public class PluginConfigServiceImpl extends BaseServiceImpl<PluginConfig, Long> implements PluginConfigService {

    @Resource
    private PluginConfigDao pluginConfigDao;

    @Transactional(readOnly = true)
    public boolean pluginIdExists(String pluginId) {
        return this.findByPluginId(pluginId)==null? false : true;
    }

    @Transactional(readOnly = true)
    public PluginConfig findByPluginId(String pluginId) {
        try {
            List<Filter> filters = new ArrayList<>();
            filters.add(Filter.eq("pluginId", pluginId));
            PluginConfig pluginConfig = pluginConfigDao.findOne(filters);
            if (pluginConfig != null) {
                return pluginConfig;
            } else {
                throw new BusinessException(ExceptionCode.PluginConfig_pluginIdNotExist);
            }
        } catch (BusinessException e) {
            logger.warn(LogHelper.logger(LogHelper.Type.WARN, ExceptionCode.PluginConfig_pluginIdNotExist, e, "service-pluginId不存在", null, true, false));
        }
        return null;
    }

    public void deleteByPluginId(String pluginId) {
        PluginConfig pluginConfig = this.findByPluginId(pluginId);
        this.delete(pluginConfig.getId());
    }

    @Transactional
    public PluginConfig save(PluginConfig pluginConfig) {
        return super.save(pluginConfig);
    }


    @Transactional
    public PluginConfig modify(PluginConfig pluginConfig) {
        return super.update(pluginConfig);
    }

    @Transactional
    public PluginConfig modify(PluginConfig pluginConfig, String... ignoreProperties) {
        return super.update(pluginConfig, ignoreProperties);
    }
}